package com.ext.aic.handler;

import android.annotation.SuppressLint;
import android.content.Context;

import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.ext.aic.adapter.ElementAdapter;
import com.ext.aic.apis.content.ProductListRequest;
import com.ext.aic.beans.Pagination;
import com.ext.aic.beans.ProductItem;
import com.ext.aic.beans.params.ProductListParams;
import com.ext.aic.components.StaggeredGridSpacingItemDecoration;
import com.ext.aic.enums.ElementLocationType;
import com.eyinfo.android_pure_utils.utils.PixelUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MineProductHandler {

    private ElementAdapter adapter;
    private ElementLocationType locationType;
    private Context context;
    private LifecycleOwner lifecycleOwner;
    private boolean isShowLoading;
    private SmartRefreshLayout refreshLayout;

    protected void onMineDataStatus(boolean empty) {

    }

    private ProductListRequest productListRequest = new ProductListRequest() {
        @SuppressLint("NotifyDataSetChanged")
        @Override
        public void onProductListSuccess(List<ProductItem> data, Pagination pagination) {
            if (adapter == null || pagination == null) {
                onMineDataStatus(true);
                return;
            }
            if (pageHandler.isEmptyData(data, pagination)) {
                onMineDataStatus(true);
            } else {
                onMineDataStatus(false);
                pageHandler.loadDone(refreshLayout, pagination, data);
                adapter.notifyDataSetChanged();
            }
        }
    };

    public void init(Context context,
                     LifecycleOwner lifecycleOwner,
                     SmartRefreshLayout refreshLayout,
                     RecyclerView recyclerView,
                     ElementLocationType locationType,
                     boolean isShowLoading) {
        this.context = context;
        this.lifecycleOwner = lifecycleOwner;
        this.refreshLayout = refreshLayout;
        this.locationType = locationType;
        this.isShowLoading = isShowLoading;
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
        recyclerView.setLayoutManager(layoutManager);
        int spacingInPx = PixelUtils.dip2px(context, 12);
        recyclerView.addItemDecoration(new StaggeredGridSpacingItemDecoration(2, spacingInPx, true));
        adapter = new ElementAdapter(context, locationType);
        adapter.setItems(pageHandler.getItems());
        recyclerView.setAdapter(adapter);
        pageHandler.registerListener(refreshLayout);
        pageHandler.refresh();
    }

    private void requestData(int page, int size) {
        ProductListParams params = new ProductListParams();
        params.setPage(page);
        params.setPageSize(size);
        if (locationType == ElementLocationType.home) {
            params.setContentStatus(Arrays.asList("self_created_approved", "others_created_approved"));
        } else if (locationType == ElementLocationType.mine) {
            params.setContentStatus(Arrays.asList("self_created_approved", "self_created_unapproved"));
        } else if (locationType == ElementLocationType.draft) {
            params.setContentStatus(Collections.singletonList("self_created_unapproved"));
        }
        productListRequest.request(context, lifecycleOwner, params, isShowLoading);
    }

    private PageHandler<ProductItem> pageHandler = new PageHandler<ProductItem>() {
        @Override
        protected void onRequestData(int page, int size) {
            requestData(page, size);
        }
    };

    public void refresh() {
        pageHandler.refresh();
    }
}
